﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TSTWBBST
    {

        public TSTWBBST()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster != null)
            {
                if (dwWB["WBBAENDZL"].ToString() != "0")
                {
                    IsKEY = false;
                }
            }
            txtLG.Focus();
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            RWAUF = RWKRS + RWLFD;
            base.QueryCommand(sender, e);
        }

        public void Copy(DataRow _dwWB)
        {
            if (_dwWB == null)
                return;
            DataRow dwData;
            while (TMV.PCS > 1)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = _dwWB.ItemArray;
                TMV.STKNR(dwData);
                TBMain.Rows.InsertAt(dwData, 0);
                dwData["WBBAENDZL"] = 1;
                TMV.InsertWBBST(dwData, app.company);
                TMV.VWBW.NWBBEW(dwData, "10", RUNID);
                TMV.PCS--;
            }
            //多增加一个虚拟的
            dwData = TBMain.NewRow();
            dwData.ItemArray = _dwWB.ItemArray;
            dwData["WBBAENDZL"] = 0;
            dwData["WBBISTKLG"] = 0;
            TMV.STKNR(dwData);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
        }

        public void Entry(DataRow _dwWB)
        {

            _dwWB["WBBAENDZL"] = 1;
            TMV.InsertWBBST(_dwWB, app.company);
            if (EKAUF.Trim().Length == 12)
            {
                _dwWB["WBBAUFTYP"] = EKAUF.Substring(0, 1);
                _dwWB["WBBAUFKRS"] = EKAUF.Substring(1, 2);
                _dwWB["WBBAUFLFD"] = EKAUF.Substring(3, 6);
                _dwWB["WBBAUFPOS"] = EKAUF.Substring(9, 3);
                TMV.VWBW.NWBBEW(_dwWB, "11", RUNID);
                PM["AUFTYP"] = EKAUF.Substring(0, 1);
                PM["KRS"] = EKAUF.Substring(1, 2);
                PM["LFD"] = EKAUF.Substring(3, 6);
                PM["POS"] = EKAUF.Substring(9, 3);
            }
            else
            {
                TMV.VWBW.NWBBEW(_dwWB, "10", RUNID);
                PM["AUFTYP"] = "".PadRight(1);
            }
            PM["RWAUF"] = RWAUF;
            PM["ORDERNUM"] = "".PadRight(20);

            TMV.VWBD.WBBCHG(PM);
        }

        private void WBBISTKLG_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;
                if (e.Key != System.Windows.Input.Key.Return)
                    return;
                if (!IsEdit)
                    return;
                TextBox txt = sender as TextBox;
                if (dwWB == null)
                {
                    MessageBox.Show("请先选择一个布卷。");
                    return;
                }
                if (txt.Text.Trim() == "0")
                {
                    MessageBox.Show("请填写长度！");
                    return;
                }
                double GM = 0.0, WID = 0.0;

                if (dwWB04 == null)
                {
                    GM = 1;
                    WID = 1;
                }
                else
                {
                    GM = System.Convert.ToDouble(dwWB04["WBGRLFD"].ToString());
                    WID = System.Convert.ToDouble(dwWB04["WBBRT"].ToString());
                }

                dwWB["WBBISTKLG"] = System.Convert.ToDouble(txtLG.Text) * 1000;
                dwWB["WBBSSTKLG"] = dwWB["WBBISTKLG"];
                dwWB["WBBSGEW"] = Helper.RoundX(System.Convert.ToDouble(txtLG.Text) * GM, 1);
                //dwWB["WBBBRGEW"] = GM;
                dwWB["WBBIBRT"] = WID;
                Entry(dwWB);//插入
                Copy(dwWB);//复制并插入
                txtLG.SelectAll();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void WBBISTKLGYD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;
                if (e.Key != System.Windows.Input.Key.Return)
                    return;
                if (!IsEdit)
                    return;
                TextBox txt = sender as TextBox;
                if (dwWB == null)
                {
                    MessageBox.Show("请先选择一个布卷。");
                    return;
                }
                if (txt.Text.Trim() == "0")
                {
                    MessageBox.Show("请填写长度！");
                    return;
                }
                double GM = 0.0, WID = 0.0;

                if (dwWB04 == null)
                {
                    GM = 1;
                    WID = 1;
                }
                else
                {
                    GM = System.Convert.ToDouble(dwWB04["WBGRLFD"].ToString());
                    WID = System.Convert.ToDouble(dwWB04["WBBRT"].ToString());
                }

                dwWB["WBBISTKLG"] = Helper.RoundX( System.Convert.ToDouble(txtLGYD.Text) *0.9144* 1000,1);
                dwWB["WBBSSTKLG"] = dwWB["WBBISTKLG"];
                dwWB["WBBSGEW"] = Helper.RoundX(System.Convert.ToDouble(txtLGYD.Text)*0.9144 * GM, 1);
                //dwWB["WBBBRGEW"] = GM;
                dwWB["WBBIBRT"] = WID;
                Entry(dwWB);//插入
                Copy(dwWB);//复制并插入
                txtLGYD.SelectAll();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {

            TMV.BATCHWBCHG();
            TMV.VWBW.POSTCHG();

            if (EKAUF.Trim().Length != 12) return;
            if (RWAUF.Length < 8) return;
            PM["RWAUF"] = RWAUF;
            PM["ORDERNUM"] = "".PadRight(20);
            PM["AUFTYP"] = EKAUF.Substring(0, 1);
            PM["KRS"] = EKAUF.Substring(1, 2);
            PM["LFD"] = EKAUF.Substring(3, 6);
            PM["POS"] = EKAUF.Substring(9, 3);
            TMV.VWBD.WBBCHG(PM);

            base.SaveCommand(sender, e);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("已处于编辑状态，请先将数据保存后才可再次进行编辑！");
                return;
            }

            MV.TBTemp = null;
            MV.TBTemp = TBMain.Copy();
            base.EditCommand(sender, e);
            IsEdit = true;
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DeleteWBBST(dwMaster, 1);
            DataRow _dw = dwMaster;
            _dw["WBBISTKLG"] = -System.Convert.ToDouble(_dw["WBBISTKLG"].ToString());
            _dw["WBBSSTKLG"] = _dw["WBBISTKLG"];
            _dw["WBBSGEW"] = -System.Convert.ToDouble(_dw["WBBSGEW"]);
            if (EKAUF.Trim().Length == 12)
            {
                _dw["WBBAUFTYP"] = EKAUF.Substring(0, 1);
                _dw["WBBAUFKRS"] = EKAUF.Substring(1, 2);
                _dw["WBBAUFLFD"] = EKAUF.Substring(3, 6);
                _dw["WBBAUFPOS"] = EKAUF.Substring(9, 3);
                TMV.VWBW.NWBBEW(_dw, "11", RUNID);
            }
            else
            {
                TMV.VWBW.NWBBEW(_dw, "10", RUNID);

            }
            /*PM["AUFTYP"] = EKAUF.Substring(0, 1);
            PM["RWAUF"] = RWAUF;
            PM["ORDERNUM"] = "".PadRight(20);
            PM["KRS"] = EKAUF.Substring(1, 2);
            PM["LFD"] = EKAUF.Substring(3, 6);
            PM["POS"] = EKAUF.Substring(9, 3);
            TMV.VWBD.WBBCHG(PM);*/

            string WBBSTKNR = dwMaster["WBBSTKNR"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            string filter = "WBBSTKNR='" + WBBSTKNR + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
            SaveCommand(sender, e);
            IsEdit = true;
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (dwWB == null)
            {
                MessageBox.Show("请先选择一个布卷。");
                return;
            }
            //dwWB["WBBISTKLG"] = System.Convert.ToDouble(txt.Text) * 1000; 
            //dwWB["WBBISTKLG"] = 0;
            //dwWB["WBBSSTKLG"] = dwWB["WBBISTKLG"];
            //dwWB["WBBSGEW"] = dwWB["WBBSGEW"];
            //dwWB["WBBBRGEW"] = dwWB["WBBBRGEW"];
            //dwWB["WBBIBRT"] = dwWB["WBBIBRT"];
            DataRow dwData;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwWB.ItemArray;
            dwData["WBBAENDZL"] = 0;
            dwData["WBBISTKLG"] = 0;
            TMV.STKNR(dwData);
            Copy(dwWB);//复制并插入
            base.CopyCommand(sender, e);
            txtLG.SelectAll();
        }

        public override void OnRWAUFChg(string value)
        {
            TBMain.Clear();
            if (value.Length < 8)
                return;
            TMV.WBDLOAD(value);
            if (FTAUTOWB)
            {
                if (TBMain.Rows.Count == 0)
                    TMV.NEWWB();
            }
            vwMaster.View.MoveCurrentToFirst();
            TBWB = null;
            TBWB = TBMain;
            base.OnRWAUFChg(value);
            txtLG.SelectAll();
        }
        public override void LOADBYAUF()
        {
            if (ORDERNUM.Length == 0)
                return;
            filter = "WBBSTKNR='" + ORDERNUM + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow dwm in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwm.ItemArray;
                dwData["WBBFILLER"] = " AND WBBSTKNR='" + ORDERNUM + "'";
                SResult = TMV.QueryWBBST(dwData, -2);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwm.ItemArray = row.ItemArray;
                }
                break;
            }
            base.LOADBYAUF();
        }
        public override void OnReload(bool value)
        {
            if (!value)
                return;
            string temp = RWAUF;
            RWAUF = "";
            RWAUF = temp;
            IsSaved = false;
            IsSaved = true;
            base.OnReload(value);
        }

    }
}
